package com.example.friendcash.database.dao.implementation.sqlite.android;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import com.example.friendcash.database.core.Event;
import com.example.friendcash.database.dao.interfaces.IEventDao;

import java.util.List;

import static com.example.friendcash.database.dao.implementation.sqlite.android.Column.*;
import static com.example.friendcash.database.dao.implementation.sqlite.android.ColumnDescription.createColumn;
import static com.example.friendcash.database.dao.implementation.sqlite.android.Nullable.NOT_NULL;
import static com.example.friendcash.database.dao.implementation.sqlite.android.PrimaryKey.primaryKey;
import static java.util.Arrays.asList;

final class EventDao extends GenericDao<Event> implements IEventDao {

    protected EventDao(Context context, String databaseName, SQLiteDaoFactory daoFactory) {
        super(context, databaseName, daoFactory);
    }

    @Override
    protected String getTableName() {
        return "events";
    }

    @Override
    protected List<ColumnDescription> getColumns() {
        return columnDescriptions;
    }

    @Override
    protected ContentValues getContentValues(Event event) {
        final ContentValues values = new ContentValues();
        values.put(NAME_COLUMN.name, event.name);
        values.put(COMMENT_COLUMN.name, event.comment);
        return values;
    }

    @Override
    protected Event createFromCursor(Cursor cursor) {
        return new Event(
                cursor.getLong(cursor.getColumnIndex(ID_COLUMN.name)),
                cursor.getString(cursor.getColumnIndex(NAME_COLUMN.name)),
                cursor.getString(cursor.getColumnIndex(COMMENT_COLUMN.name))
        );
    }

    @Override
    protected Column getIdColumnName() {
        return ID_COLUMN;
    }

    private final List<ColumnDescription> columnDescriptions = asList(
            createColumn(ID_COLUMN, primaryKey(true, NOT_NULL)),
            createColumn(NAME_COLUMN),
            createColumn(COMMENT_COLUMN)
    );

}
